package com.jinlejia.erp.ui.customer;


import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.jinlejia.erp.R;
import com.jinlejia.erp.adapter.Entering2Adapter;
import com.jinlejia.erp.databinding.ActivityEntering2Binding;
import com.jinlejia.erp.ui.house.HouseDetailActivity;
import com.jinlejia.erp.utils.ActivityCollector;

import org.simple.eventbus.EventBus;
import org.simple.eventbus.Subscriber;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * 录带看第二步（activity）
 */

public class EnteringVisitActivity2 extends AppCompatActivity {


    private Entering2Adapter adapter;
    String type;

    private ActivityEntering2Binding binding;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_entering2);
        binding.setEvent(this);
        EventBus.getDefault().register(this);
        EventBus.getDefault().registerSticky(this);
        ActivityCollector.addActivity2(this);
        initView();
        type = getIntent().getStringExtra("type");
    }

    /**
     * 初始化视图
     */
    private void initView() {

        binding.enteringRecyclerView.setHasFixedSize(true);
        binding.enteringRecyclerView.setLayoutManager(new LinearLayoutManager(this));

        ArrayList<Map<String, Object>> dataList = new ArrayList<>();
//        setlist(dataList);

        adapter = new Entering2Adapter(dataList);
        binding.enteringRecyclerView.setAdapter(adapter);
        adapter.setOnHouseItemClickListener(code -> {
            Intent hIntent = new Intent(EnteringVisitActivity2.this, HouseDetailActivity.class);
            hIntent.putExtra("code", code);
            hIntent.putExtra("type", getIntent().getStringExtra("type"));
            startActivity(hIntent);
        });
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back_btn:
                finish();
                break;
            case R.id.bt_up:
                //上一步
                finish();
                break;
            case R.id.bt_next:
                //下一步
                Map<String, Object> map = new HashMap<>();
                map.put("csCode", getIntent().getStringExtra("csCode"));
                map.put("type", getIntent().getStringExtra("type"));
                map.put("startTime", getIntent().getStringExtra("startTime"));
                map.put("endTime", getIntent().getStringExtra("endTime"));
                map.put("houseList", adapter.getData());
                map.put("name", getIntent().getStringExtra("name"));
                map.put("sex", getIntent().getStringExtra("sex"));
                EventBus.getDefault().postSticky(map, "TAG_ENTER_DATA");
                Intent intent = new Intent(this, EnteringVisitActivity3.class);
                startActivity(intent);
                break;
            case R.id.compile:
                Intent intentCom = new Intent(this, AddHouseActivity.class);
                intentCom.putExtra("type", getIntent().getStringExtra("type"));
                //遍历约看房源，获取房源ID
                ArrayList<String> iDList = new ArrayList<>();
                for (Map<String, Object> map1 : adapter.getData()) {
                    iDList.add(map1.get("rscode").toString());
                }
                intentCom.putExtra("ids", iDList);
                startActivity(intentCom);
                break;
            case R.id.add_house:
                //添加房源
                Intent intent1 = new Intent(this, AddHouseActivity.class);
                intent1.putExtra("type", getIntent().getStringExtra("type"));
                startActivity(intent1);
                break;
            default:
                break;
        }
    }

    @Subscriber(tag = "TAG_FINISH_CHECKED")
    public void data(ArrayList<Map<String, Object>> data) {
        if (data.size() > 0) {
            binding.enteringRecyclerView.setVisibility(View.VISIBLE);
            binding.addHouseLayout.setVisibility(View.GONE);
            binding.houseCount.setText("带看房源(" + data.size() + ")");
            ArrayList<Map<String, Object>> cData = new ArrayList<>();
            for (int i = 0; i < data.size(); i++) {
                Map<String, Object> sMap = data.get(i);
                Map<String, Object> map = new HashMap<>();
                map.put("address", sMap.get("estate").toString().concat("  ").concat(sMap.get("houseType").toString()));
                if (type.equals("sale") || type.equals("1")) {
                    map.put("info", String.valueOf(sMap.get("square")).concat("㎡|").concat(String.valueOf(sMap.get("unitPrice"))).concat("万/㎡"));
                    map.put("money", String.valueOf(sMap.get("salePrice")).concat("万"));
                } else {
                    map.put("info", String.valueOf(sMap.get("square")).concat("㎡"));
                    map.put("money", String.valueOf(sMap.get("rentPrice")).concat("元"));
                }

                map.put("rscode", sMap.get("rscode"));
                map.put("image", sMap.get("image"));
                cData.add(map);
            }
            adapter.setData(cData);
        }

    }

    @Subscriber(tag = "TAG_YUEKAN_HOUSE_LIST")
    private void yueKanInfo(Map<String, Object> map1) {
        EventBus.getDefault().removeStickyEvent(map1.getClass(), "TAG_YUEKAN_HOUSE_LIST");
        binding.enteringRecyclerView.setVisibility(View.VISIBLE);
        binding.addHouseLayout.setVisibility(View.GONE);
        binding.houseCount.setText("带看房源(" + map1.get("countHouse") + ")");
        type = map1.get("type").toString();
        ArrayList<Map<String, Object>> data = JSON.parseObject(map1.get("houseList").toString(), new TypeReference<ArrayList<Map<String, Object>>>() {
        });
        ArrayList<Map<String, Object>> cData = new ArrayList<>();
        for (int i = 0; i < data.size(); i++) {
            Map<String, Object> sMap = data.get(i);
            Map<String, Object> map = new HashMap<>();
            map.put("address", sMap.get("estate").toString().concat("  ").concat(sMap.get("houseType").toString()));
            if (map1.get("type").equals("1") || map1.get("type").equals("sale")) {
                DecimalFormat df = new DecimalFormat("#.00");
                Double unitPrice = Double.parseDouble(map.get("price").toString()) / Double.parseDouble(map.get("square").toString()) * 10000;
                map.put("info", String.valueOf(sMap.get("square")).concat("㎡|").concat(df.format(unitPrice)).concat("万/㎡"));
                map.put("money", String.valueOf(sMap.get("price")).concat("万"));
            } else {
                map.put("info", String.valueOf(sMap.get("square")).concat("㎡"));
                map.put("money", String.valueOf(sMap.get("price")).concat("元"));
            }

            map.put("rscode", sMap.get("rsCode"));
            map.put("image", sMap.get("imgUrl"));
            cData.add(map);
        }
        adapter.setData(cData);

    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        ActivityCollector.removeActivity2(this);
        super.onDestroy();
    }
}
